package com.paul;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.rpc.RpcContext;
import com.paul.service.TestService;

@SpringBootApplication
//@DubboComponentScan(basePackages = "com.paul")
@RequestMapping
public class ConsumerTestApp {

	private static Logger logger = LoggerFactory.getLogger(ConsumerTestApp.class);
	
	@Autowired
	@Qualifier("test2Service")
    private TestService test2Service;
	
	@Autowired
	@Qualifier("testService")
    private TestService testService;
	
	@Autowired
	private ApplicationConfig applicationConfig;
	
	@Autowired
	private RegistryConfig registryConfig;
	
	private int count = 0;
	
	@RequestMapping("/test")
	@ResponseBody
	public String test() {
		count++;
		// 当前应用配置
		/*ApplicationConfig application = new ApplicationConfig();
		application.setName("providers");*/

		// 连接注册中心配置
		/*RegistryConfig registry = new RegistryConfig();
		registry.setProtocol("zookeeper");
		registry.setAddress("192.168.56.2:2181");*/
		//registry.set
		//registry.setUsername("aaa");
		//registry.setPassword("bbb");

		// 注意：ReferenceConfig为重对象，内部封装了与注册中心的连接，以及与服务提供方的连接

		
		
		// 引用远程服务
		/*ReferenceBean<TestService> reference = new ReferenceBean<TestService>(); // 此实例很重，封装了与注册中心的连接以及与提供者的连接，请自行缓存，否则可能造成内存和连接泄漏
		reference.setApplication(applicationConfig);
		reference.setRegistry(registryConfig); // 多个注册中心可以用setRegistries()
		reference.setInterface(TestService.class);
		//reference.setInterface("com.paul.service.TestService");
		reference.setCheck(false);
		reference.setVersion("1.0");
		if(count % 2 == 0) {
			reference.setGroup("test");
		}else {
			reference.setGroup("test2");
		}
		
		TestService service = reference.get();
		
		return service.show("abcl"+count);*/
		return "";
	}
	
	@RequestMapping("/test2")
	@ResponseBody
	public String test2() {
		count++;
		String str = "";
		if(count % 2 == 0) {
			str = testService.show("zbl"+count);
		}else {
			str = test2Service.show("zbl"+count);
		}
		return str;
	}

	@RequestMapping("/test3")
	@ResponseBody
	public String test3(){
		String str = "这是在develop开发的";
		return str;
	}

	@RequestMapping("/test5")
	@ResponseBody
	public String test5(){
		String str = "这是在master开发的";
		return str;
	}

	@RequestMapping("/test6")
	@ResponseBody
	public String test6(){
		String str = "这是在master开发的";
		return str;
	}

	@RequestMapping("/test7")
	@ResponseBody
	public String test7(){
		String str = "这是在develop开发的;推送给远程的develop";
		return str;
	}

	@RequestMapping("/test8")
	@ResponseBody
	public String test8(){
		String str = "这是在develop开发的;推送给远程的develop";
		return str;
	}

	@RequestMapping("/test9")
	@ResponseBody
	public String test9(){
		String str = "这是在master开发的";
		return str;
	}

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ConsumerTestApp.class);
		ApplicationContext ctx = app.run(args);
		//AnnotationConsumeService service = ctx.getBean(AnnotationConsumeService.class);
		//service.test();
		Environment env = ctx.getEnvironment();
		//startDaemonAwaitThread();
		logger.info("启动成功...");
		/*TestService service = ctx.getBean(TestService.class);
		System.out.println(service);*/
	}
	
	//等待线程
	public static void startDaemonAwaitThread() {
		Thread awaitThread = new Thread("container") {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10000);
					} catch (InterruptedException ex) {
					}
				}
			}

		};
		awaitThread.setContextClassLoader(ConsumerTestApp.class.getClassLoader());
		awaitThread.setDaemon(false);
		awaitThread.start();
	}
}
